package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.service.CheckgroupService;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/checkGroup")
public class CheckgroupController {
    // todo 删除时有外键,删不动
    @Reference
    private CheckgroupService checkgroupService;


    @RequestMapping("/findAll/{checkGroupId}")
    public Result findAll(@PathVariable("checkGroupId") Integer checkGroupId){
        List<CheckGroup> allCheckGroup = checkgroupService.findAllCheckGroup(checkGroupId);
        return new Result(true,"",allCheckGroup);
    }
    @PreAuthorize("hasAuthority('CHECKGROUP_DELETE')")
    @RequestMapping("/deleteById/{id}")
    public Result deleteById( @PathVariable Integer id){
        System.out.println(id);
        try {
//            Integer id  = Integer.parseInt(ids);
           checkgroupService.delete(id);
            return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
        }catch (RuntimeException e){
            return new Result(false,e.getMessage());
        }
        catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.DELETE_CHECKGROUP_FAIL);
        }

    }

    @RequestMapping("/edit")
    public Result edit(@RequestBody Map<String,Object> map){
        System.out.println(map);
        try {
            checkgroupService.edit(map);
            return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.EDIT_CHECKGROUP_FAIL);
        }
    }


    @RequestMapping("/add")
    public Result add(@RequestBody Map<String,Object> map){

        try {
            checkgroupService.add(map);
            return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.ADD_CHECKGROUP_FAIL);
        }
    }

    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean){
        System.out.println("hello");
        //  如果用了分组,再第二页会搜不到第一页中的数据
        PageResult page = checkgroupService.findPage(queryPageBean);
        return page;
    }

}
